#ifndef SONGMODEL_H
#define SONGMODEL_H

#include <QObject>
#include <QMap>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include "EZTallyObject.h"
#include "commonTools/define.h"
#include <QSqlQuery>

#define songHash_ape "hash_ape"
#define songHash_320 "320hash"
#define songHash_m4a "m4ahash"
#define songHash_normal "hash"
#define songHash_localPath "localPath"

#define SongModel_DB_TABLE "songmodel_table"

class SongModel : public QObject , public EZTally::EZTallyObject
{
    Q_OBJECT
protected:
    virtual ~SongModel();
public:
     SongModel();

     struct Quality //
     {
     public:
         QString url;
         QString hash;
         long long size;
         double timeLength;
         QString extName;
     public:
         Quality(){url=QString();hash=QString();size=0;timeLength=0;}
         Quality(const QString &h,long long s){
             hash = h;
             size = s;
             timeLength = 0;
             url=QString();
         }
         QJsonObject tojson(bool keepUrl=false);
         int initWithJson(QJsonObject &obj);
     };

private:
     int _dbid;
    QString _title;//显示名字

    double _time;
    int _sid;
    QMap<QString,SongModel::Quality> _qualityMap;

private:
    QNetworkAccessManager _netManager;
    QMap<QString,QNetworkRequest> _requestKeys;
public:

    QString title() const;
    void setTitle(const QString &title);

    double time() const;
    void setTime(double time);
    int sid() const;
    void setSid(int sid);

    QStringList qualityKeys();
    SongModel::Quality qualityWithKey(QString &key);
    void setQualityWithKey(QString &key,SongModel::Quality &quality);
//    QMap<const QString, SongModel::Quality> qualityMap() const;
//    void setQualityMap(const QMap<const QString, SongModel::Quality> &qualityMap);

    QString getUrlWith(const QString &key,int *getType);

    int dbid() const;
    void setDbid(int dbid);

    void saveToDB(bool isReplace=false);
    void initSongWith(QSqlQuery &query);

    static void checkDBState();
private:

    QString qualityToString();

signals:
    void sigSongUrlChange(QString &key,int error);
private slots:
    void onGetSongUrl(QNetworkReply*);
};

#endif // SONGMODEL_H
